package pl.edu.mimuw.mas.chart;

import java.io.Serializable;

import pl.edu.mimuw.mas.exception.WrongAreaException;

/**
 * Obszar ograniczony prostokątem.
 */
public class Area implements Serializable {
	private static final long serialVersionUID = -8776470842046858977L;

	public static final int X0 = 0;
	public static final int Y0 = 1;
	public static final int X1 = 2;
	public static final int Y1 = 3;
	
	public static boolean checkArea(int x0, int y0, int x1, int y1) {
		return !(x0 > x1 || y0 > y1 || x0 < 0 || y0 < 0);
	}
	
	public static Area join(Area a0, Area a1) throws WrongAreaException {
		return new Area(a0.area[X0], a0.area[Y0], a1.area[X1], a1.area[Y1]);
	}
	
	private final int[] area;

	public Area(int x0, int y0, int x1, int y1) throws WrongAreaException {
		if (!checkArea(x0, y0, x1, y1)) {
			throw new WrongAreaException();
		}

		this.area = new int[4];

		this.area[X0] = x0;
		this.area[Y0] = y0;
		this.area[X1] = x1;
		this.area[Y1] = y1;
	}

	public boolean isInArea(int x, int y) {
		return x >= area[X0] && x <= area[X1] && y >= area[Y0] && y <= area[Y1]; 	
	}
		
	public int[] getArea() {
		return area;
	}
}
